package cn.jwelll.bxcstock.ui.carin.preview

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import cn.jwelll.bxcstock.R
import cn.jwelll.bxcstock.base.DataBindingActivity
import cn.jwelll.bxcstock.common.ConstValue
import cn.jwelll.bxcstock.common.mStartActivity
import cn.jwelll.bxcstock.ui.adapter.CommonRecyclerAdapter
import cn.jwelll.bxcstock.ui.carin.upload.CarInUploadActivity
import cn.jwelll.bxcstock.ui.view.recycleview.HorizontalDividerDecoration
import cn.jwelll.bxcstock.ui.view.recycleview.WrapContentManager
import cn.jwelll.bxcstock.viewmodel.itemmodel.ItemCarInPreviewModel
import cn.jwelll.bxcstock.viewmodel.viewmodel.CarInPreviewModel
import kotlinx.android.synthetic.main.activity_carin_preview.*
import cn.jwelll.bxcstock.util.*
import com.google.gson.Gson
import kotlinx.android.synthetic.main.layout_title_double.*

/**
 * 汽运装车 火车装车 的操作界面第一步
 */
class CarInPreviewActivity : DataBindingActivity<CarInPreviewModel, CarInPreviewContract.View, CarInPreviewPresenter>(),
    CarInPreviewContract.View {
    var mScanCode=""
    var mCarInPreviewModel: CarInPreviewModel? = null

    override fun createPresenter() = CarInPreviewPresenter(this@CarInPreviewActivity)

    override fun carScanCodeSuccess(model: CarInPreviewModel) {
        mCarInPreviewModel = model
        tv_start_confirm.visibility=View.VISIBLE
        adapter.clear()
        adapter.insertData(model.materials)
        tv_name_carnum.text = mCarInPreviewModel?.driverName + " " + mCarInPreviewModel?.carNum
        tv_title_location.text = mCarInPreviewModel?.qeueuNumber + "/" + mCarInPreviewModel?.loadingPoint
        tv_title_single_count.text = mCarInPreviewModel?.weight.toString()
        for((index,value) in mCarInPreviewModel?.materials?.withIndex()!!)
        {
            value.index=index+1
        }
        notifyModel(mCarInPreviewModel!!)
        cart_view.visibility = View.VISIBLE
        tv_help_info.visibility = View.GONE
    }

    private val adapter by lazy {
        CommonRecyclerAdapter<ItemCarInPreviewModel>(
            this,
            cn.jwelll.bxcstock.R.layout.item_carin_preview,
            ArrayList()
        ) { v, t, _ ->
        }
    }

    internal var receiver: BroadcastReceiver = object : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {
            LogUtil.d("receiver-1-${intent.action}")
            LogUtil.d("receiver-2-${intent.getStringExtra("BARCODE")}")
            if (intent.action == "com.barcode.sendBroadcast") {
                mScanCode=intent.getStringExtra("BARCODE")
                if(mScanCode.length==6)
                {
                    showProgress("")
                    mPresenter.carScanCode(mScanCode)
                }else{
                    mScanCode=""
                    showToast("请扫描正确的提货码凭证！")
                }
            }
        }
    }

    override fun getContentView() = R.layout.activity_carin_preview

    override fun getTitleString() = ""

    override fun initData() {
        super.initData()
        titleString = "${SPUtils.getString(this,ConstValue.WARE_HOUSE_NAME,"")}汽车装车出库"
    }

    override fun onResume() {
        super.onResume()
        ScanHelper.setScanSwitchLeft(this, true);
        ScanHelper.setScanSwitchRight(this, true);
        ScanHelper.setBarcodeReceiveModel(this, 2);

        var intentFilter = IntentFilter();
        intentFilter.addAction("com.barcode.sendBroadcast");
        registerReceiver(receiver, intentFilter);
    }


    override fun initView() {
        super.initView()
        initRecycleView()

        ll_top_carin.visibility = View.VISIBLE
        ll_top_trainin.visibility = View.GONE
        layout_bottom_single.visibility = View.VISIBLE
        layout_bottom_double.visibility = View.GONE

    }


    override fun initEvent() {
        super.initEvent()

        tv_start_confirm.setOnClickListener {
            if (tv_help_info.visibility != View.VISIBLE) {
                mStartActivity(
                    CarInUploadActivity::class.java,
                    Pair("model", Gson().toJson(mCarInPreviewModel)),
                    Pair("scancode",mScanCode),
                    Pair(ConstValue.INCAR_SHOW_TYPE, CarInUploadActivity.TYPE_CAR_IN)
                )
            } else {
                showToast("请先扫描二维码！")
            }
        }
    }

    private fun initRecycleView() {
        val mLinearLayoutManager = WrapContentManager(this, LinearLayoutManager.VERTICAL, false)
        recycler_view.layoutManager = mLinearLayoutManager
        recycler_view.addItemDecoration(
            HorizontalDividerDecoration(
                this,
                cn.jwelll.bxcstock.R.drawable.divider_horizontal_2_f3f6fa,
                DisplayUtil.dip2px(this, 10f)
            )
        )
        recycler_view.setHasFixedSize(true)
        recycler_view.setLoadingMoreEnabled(false)
        recycler_view.setPullRefreshEnabled(false)
        recycler_view.refreshComplete()
        recycler_view.loadMoreComplete()

        recycler_view.adapter = adapter
    }

    override fun onPause() {
        super.onPause()
        unregisterReceiver(receiver)
    }
}